/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model;


import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Eleftherios
 */
@Entity
@Table(name = "ARTIST")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Artist.findAll", query = "SELECT a FROM Artist a"),
    @NamedQuery(name = "Artist.findByArtistId", query = "SELECT a FROM Artist a WHERE a.artistId = :artistId"),
    @NamedQuery(name = "Artist.findByFirstName", query = "SELECT a FROM Artist a WHERE a.firstName = :firstName"),
    @NamedQuery(name = "Artist.findByLastName", query = "SELECT a FROM Artist a WHERE a.lastName = :lastName"),
    @NamedQuery(name = "Artist.findByArtisticName", query = "SELECT a FROM Artist a WHERE a.artisticName = :artisticName"),
    @NamedQuery(name = "Artist.findBySex", query = "SELECT a FROM Artist a WHERE a.sex = :sex"),
    @NamedQuery(name = "Artist.findByBirthday", query = "SELECT a FROM Artist a WHERE a.birthday = :birthday"),
    @NamedQuery(name = "Artist.findByBirthplace", query = "SELECT a FROM Artist a WHERE a.birthplace = :birthplace")})
public class Artist implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ARTIST_ID")
    private Integer artistId;
    @Basic(optional = false)
    @Column(name = "FIRST_NAME")
    private String firstName;
    @Basic(optional = false)
    @Column(name = "LAST_NAME")
    private String lastName;
    @Basic(optional = false)
    @Column(name = "ARTISTIC_NAME")
    private String artisticName;
    @Basic(optional = false)
    @Column(name = "SEX")
    private String sex;
    @Column(name = "BIRTHDAY")
    @Temporal(TemporalType.DATE)
    private Date birthday;
    @Column(name = "BIRTHPLACE")
    private String birthplace;
    @ManyToMany(mappedBy = "artistList")
    private List<MusicGroup> musicGroupList;
    @JoinTable(name = "ARTIST_OWNER", joinColumns = {
        @JoinColumn(name = "ARTIST", referencedColumnName = "ARTIST_ID")}, inverseJoinColumns = {
        @JoinColumn(name = "ALBUM", referencedColumnName = "ALBUM_ID")})
    @ManyToMany
    private List<Album> albumList;
    @JoinColumn(name = "MUSIC_GENRE", referencedColumnName = "MUSIC_GENRE_ID")
    @ManyToOne(optional = false)
    private MusicGenre musicGenre;

    public Artist() {
    }

    public Artist(Integer artistId) {
        this.artistId = artistId;
    }

    public Artist(Integer artistId, String firstName, String lastName, String artisticName, String sex) {
        this.artistId = artistId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.artisticName = artisticName;
        this.sex = sex;
    }

    public Integer getArtistId() {
        return artistId;
    }

    public void setArtistId(Integer artistId) {
        this.artistId = artistId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getArtisticName() {
        return artisticName;
    }

    public void setArtisticName(String artisticName) {
        this.artisticName = artisticName;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getBirthplace() {
        return birthplace;
    }

    public void setBirthplace(String birthplace) {
        this.birthplace = birthplace;
    }

    @XmlTransient
    public List<MusicGroup> getMusicGroupList() {
        return musicGroupList;
    }

    public void setMusicGroupList(List<MusicGroup> musicGroupList) {
        this.musicGroupList = musicGroupList;
    }

    @XmlTransient
    public List<Album> getAlbumList() {
        return albumList;
    }

    public void setAlbumList(List<Album> albumList) {
        this.albumList = albumList;
    }

    public MusicGenre getMusicGenre() {
        return musicGenre;
    }

    public void setMusicGenre(MusicGenre musicGenre) {
        this.musicGenre = musicGenre;
    }
    
    
    public List<Artist> read(Boolean All, String SQL)   {      // All = False, SQL = "" διαβάζει μία εγγραφή
                                                               // All = True, SQL = "" διαβάζει όλους τους Artists
                                                               // All = True, False, SQL != "" διαβάζει απο το SQL
       String Sql="Select * From Artist Order By Last_Name, First_Name";
       if(All==false){
           Sql = "Select * From Artist Where Artist_Id = " + artistId.toString();
       }
       if (SQL != "") { Sql=SQL; }
       
       dataLayer DataLayer = new dataLayer();
       ResultSet res = DataLayer.ExecuteQuery(Sql);
       List<Artist> list = new ArrayList<>();
       try
            {
                     this.artisticName="";
                     this.birthday=null;
                     this.birthplace="";
                     this.firstName="";
                     this.lastName="";
                     this.sex="";
                     this.musicGenre=null;
                 while (res.next()) {
                     
                    this.artistId=res.getInt("Artist_ID");
                    this.artisticName=res.getString("Artistic_Name");
                    this.birthday=res.getDate("Birthday");
                    this.birthplace=res.getString("Birthplace");
                    this.firstName=res.getString("First_Name");
                    this.lastName=res.getString("Last_Name");
                    this.sex=res.getString("Sex");
                     
                    MusicGenre tmpMG = new MusicGenre();
                    tmpMG.setMusicGenreId(res.getInt("Music_Genre"));
                    tmpMG.read(Boolean.FALSE);
                    this.musicGenre=tmpMG;
                     
                    Artist row = new Artist();
                    row.setArtistId(artistId);
                    row.setArtisticName(artisticName);
                    row.setBirthday(birthday);
                    row.setBirthplace(birthplace);
                    row.setFirstName(firstName);
                    row.setFirstName(firstName);
                    row.setLastName(lastName);
                    row.setSex(sex);
                    row.setMusicGenre(musicGenre);
                                       
                    list.add(row);                     
                  }
            }
             catch ( SQLException err ) {
                System.out.println( err.getMessage( ) );
             }  
       return list;
    }
    
     public long update(){
       String MyDate = Integer.toString(birthday.getMonth()+1) + "/" + Integer.toString(birthday.getDate()) + "/" + Integer.toString(birthday.getYear()+1900);
       String Sql = "Update Artist Set First_Name = '" + firstName + "'" +
                                     ", Last_Name = '" + lastName + "'" +
                                     ", Artistic_Name = '" + artisticName + "'" +
                                     ", Sex = '" + sex + "'" +
                                     ", Birthday = '" + MyDate + "'" +
                                     ", Birthplace = '" + birthplace + "'" +
                                     ", music_genre = " + musicGenre.getMusicGenreId().toString() + 
                     " Where Artist_Id = " + artistId.toString();
       dataLayer DataLayer = new dataLayer();
       return DataLayer.ExecuteUpdate(Sql);
    }
     
   
    public long insert(){
          String MyDate = Integer.toString(birthday.getMonth()+1) + "/" + Integer.toString(birthday.getDate()) + "/" + Integer.toString(birthday.getYear()+1900);                  
          String Sql = "Insert Into Artist (First_Name , " +
                                     "Last_Name, " +
                                     "Artistic_Name, " +
                                     "Sex, " + 
                                     "Birthday, " +
                                     "Birthplace, " +
                                     "music_genre) Values (" +
                                     "'" + firstName + "'" +
                                     ",'" + lastName + "'" +
                                     ",'" + artisticName + "'" +
                                     ",'" + sex + "'" +
                                     ",'" + MyDate + "'" +
                                     ",'" + birthplace + "'" +
                                     "," + musicGenre.getMusicGenreId().toString() + ")";
       dataLayer DataLayer = new dataLayer();
       return DataLayer.ExecuteUpdate(Sql);
    }
   
    public long InsertMusicGroup(int Music_Group_Id, int Artist_Id){
          String Sql = "Insert Into Has_Members (Music_Group , " +
                                     "Artist) Values (" +
                                     Integer.toString(Music_Group_Id) + 
                                     "," + Integer.toString(Artist_Id) + ")";
       dataLayer DataLayer = new dataLayer();
       return DataLayer.ExecuteUpdate(Sql);
    }
       
    public long delete(){
       String Sql = "Delete From  Artist Where Artist_Id = " + artistId.toString();
       dataLayer DataLayer = new dataLayer();
       return DataLayer.ExecuteUpdate(Sql);
    }
    
    public long deleteMusicGroup(int Music_Group_Id, int Artist_Id){
       String Sql = "Delete From  Has_Members Where Music_Group = " + 
                     Integer.toString(Music_Group_Id) + " And Artist = " +
                     Integer.toString(Artist_Id);
       dataLayer DataLayer = new dataLayer();
       return DataLayer.ExecuteUpdate(Sql);
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (artistId != null ? artistId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Artist)) {
            return false;
        }
        Artist other = (Artist) object;
        if ((this.artistId == null && other.artistId != null) || (this.artistId != null && !this.artistId.equals(other.artistId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "model.Artist[ artistId=" + artistId + " ]";
    }
    
}
